layui.use(['jquery','table','layer','form','laydate'],function(){
    var $ = layui.jquery;
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var laydate = layui.laydate;


    laydate.render({
        elem:"#createDate",
        type:"date",
        trigger:'click'
    });

    select("","","","");
    function select(deptName,trueName,createDate,status){
        table.render({
            elem:"#root",
            url:"/DeptList/selectJsDeptList?deptName="+deptName+"&trueName="+trueName+"&createDate="+createDate+"&status="+status,
            toolbar:"#toptool",
            defaultToolbar: ['','',''],
            page:true,
            limit:5,
            limits:[5,10,15,20],
            size: 'lg',
            cols:[[
                {type:"checkbox",align:"center"},
                {type:"numbers",title:"序号",align:"center"},
                {field:"deptName",title:"部门名称",align:"center"},
                {field:"deptDescirbe",title:"部门描述",align:"center"},
                {field:"trueName",title:"创建人",align:"center"},
                {field:"createDate",title:"创建时间",align:"center"},
                {field:"status",title:"启用|停用",align:"center",templet:function (data) {
                       if(data.status == 1){
                           var radio = '<a href="javascript:updateStatus('+data.id+','+data.status+');"><input class="layui-input swi" type="checkbox" name="sate" lay-skin="switch" lay-text="启用|停用" lay-filter="switchTest" value="' + data.status + '" checked></a>';
                       }else{
                           var radio = '<a href="javascript:updateStatus('+data.id+','+data.status+');"><input class="layui-input swi" type="checkbox" name="sate" lay-skin="switch" lay-text="启用|停用" lay-filter="switchTest" value="' + data.status + '"></a>';
                       }
                       return radio;
                    }},
                {title:"操作",align:"center",templet:function (data) {
                        return "<div>"+
                            "<a href='javascript:bianji(" + data.id + ");' style='color: #0EAA9E;margin-right: 10px'>编辑</a>" +
                            "<a href='javascript:shanchu(" + data.id + ");' style='color: #0EAA9E;margin-right: 10px'>删除</a>" +
                            "</div>";
                    }}
            ]]
        })
    };
    //搜索
    $("#sousuo").click(function () {
        var deptName = $("#deptName").val();
        var trueName = $("#trueName").val();
        var createDate = $("#createDate").val();
        var status = $("#status").val();
        select(deptName,trueName,createDate,status);
    });
    //重置
    $("#chongzhi").click(function () {
        select("","","","");
    });

    //改变状态
    updateStatus = function updateStatus(id,status) {
        if(status=="1"){
            var status2 = 2;
        }else{
            var status2 = 1;
        }
        var updateDate = new Date();
        updateDate = getTime(updateDate);
        var json={
            id:id,
            status:status2,
            updateId:localStorage.getItem("createId"),
            updateDate:updateDate
        }
        $.ajax({
            type:"post",
            dataType:"json",
            data:json,
            url:"/DeptList/updateJsDeptListStatus",
            success:function (data) {
                if(data){
                    layer.msg("修改成功");
                    location.reload();
                }else{
                    layer.msg("修改失败");
                }
            }
        })
    };
    //编辑信息
    bianji = function bianji(id){
        $.ajax({
            type:"post",
            javaType:"json",
            data:{"id":id},
            url:"/DeptList/selectJsDept",
            success:function (data) {
                //console.log(data);
                edit(data);
            }
        })
    };
    function edit(data){
        var obj;
        layer.open({
            type:2,
            shode:0,
            maxmin:true,
            title:"编辑部门信息",
            content:"/jurisdiction/jurisdiction-dept-list-edit.html",
            area:["600px","440px"],
            btn:["确定","取消"],
            yes:function(index){
                var updateDate = new Date();
                updateDate = getTime(updateDate);
                var json = {
                    id:obj.find("#id").val(),
                    deptName:obj.find("#deptName").val(),
                    deptDescirbe:obj.find("#deptDescirbe").val(),
                    updateId:localStorage.getItem("createId"),
                    updateDate:updateDate
                };
                $.ajax({
                    type:"post",
                    dataType:"json",
                    data:json,
                    url:"/DeptList/updateJsDept",
                    success:function (data) {
                        if(data){
                            layer.msg("编辑成功");
                            layer.close(index);
                        }else{
                            layer.msg("编辑失败");
                        }
                        location.reload();
                    }
                })
            },btn2:function(index){
              layer.close(index);
            },
            success:function (layero,index) {
                obj = layero.find("iframe").contents();
                obj.find("#id").val(data.data.id);
                obj.find("#deptName").val(data.data.deptName);
                obj.find("#trueName").val(data.data.trueName);
                obj.find("#createDate").val(data.data.createDate);
                obj.find("#deptDescirbe").val(data.data.deptDescirbe);
            }
        })
    };
    //删除
    shanchu = function shanchu(id) {
        layer.confirm('是否确定删除此条数据', {
                icon: 3, title: '提示'
            },
            function (index) {
                //通过id删除表格数据
                $.ajax({
                    type:"post",
                    dataType:"json",
                    data:{"id":id},
                    url:"/DeptList/delJsDeptList",
                    success:function (data) {
                        if(data){
                            layer.msg("删除成功");
                            location.reload();
                        }else{
                            layer.msg("删除成功");
                        }
                    }
                });
            }
        )
    };

    //添加
    tianjia = function tianjia() {
        var obj;
        layer.open({
            type:2,
            shode:0,
            maxmin:true,
            content:"/jurisdiction/jurisdiction-dept-list-add.html",
            area:["600px","440px"],
            btn:["确定","取消"],
            yes:function (index) {
                var json = {
                    createId:localStorage.getItem("createId"),
                    deptName:obj.find("#deptName").val(),
                    status:obj.find("#status").val(),
                    createDate:obj.find("#createDate").val(),
                    deptDescirbe:obj.find("#deptDescirbe").val()
                }
                //console.log(json);
                $.ajax({
                    type:"post",
                    dataType:"json",
                    data:json,
                    url:"/DeptList/addJsDeptList",
                    success:function (data) {
                        if(data){
                            layer.msg("添加成功");
                            layer.close(index);
                            location.reload();
                        }else{
                            layer.msg("添加成功");
                            layer.close(index);
                        }
                    }
                });
            },btn2:function (index) {
                layer.close(index);
            },success:function (layero,index) {
                obj = layero.find("iframe").contents();
            }
        })
    };

    //顶部时间
    function getTime(date){
        var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
        var month = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
        var today = date.getDate(); //获取当前日(1-31)
        var hour = date.getHours(); //获取当前小时数(0-23)
        var minute = date.getMinutes(); //获取当前分钟数(0-59)
        var second = date.getSeconds(); //获取当前秒数(0-59)
        var nowTime;
        nowTime = year+'-'+fillZero(month)+'-'+fillZero(today)+' '+fillZero(hour)+':'+fillZero(minute)+':'+fillZero(second);
        return nowTime;
    };

    //时间的为零操作
    function fillZero(str){
        var realNum;
        if(str<10){
            realNum	= '0'+str;
        }else{
            realNum	= str;
        }
        return realNum;
    };
    $("#shuaxin").click(function () {
        location.reload();
    });
    //跳转
    goBack=function goBack(){
        //通过页面查找iframe
        parent.$('#recordLog').val($('#examine').text());
        parent.before();
        return false;
    };
});